package cn.sccl.dirms.monitor.controller;

import cn.sccl.common.zabbix.Zabbix;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 动作相关API
 * Created by juleiy on 2017/5/19.
 */
@RestController
@RequestMapping(value = "/dirms/monitor/action")
public class ActionController {
    /**
     * 获取(trigger类型)动作列表
     */
    @RequestMapping(value = "/get", method = RequestMethod.GET)
    public List<Map<String, Object>> getActionList() {
        Zabbix zabbix = Zabbix.getZabbix();
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("output", "extend");
        params.put("selectOperations", "extend");
        params.put("selectRecoveryOperations", "extend");
        params.put("selectFilter", "extend");
        Map<String, Object> filterMap = new HashMap<>();
        filterMap.put("eventsource", 0);
        params.put("filter", filterMap);
        Map<String, Object> result = zabbix.call("action.get", params);
        List<Map<String, Object>> mapList = (List<Map<String, Object>>) result.get("result");
        List<Map<String, Object>> conditions;

        for (Map<String, Object> map : mapList) {
            conditions = (List<Map<String, Object>>) (((Map<String, Object>) map.get("filter")).get("conditions"));
            for (Map<String, Object> condition : conditions) {
                this.formatCondition(condition);
            }
        }
        return mapList;
    }

    /**
     * 格式化条件展示信息 条件类型+运算符+条件值
     *
     * @param filter
     */
    private void formatCondition(Map<String, Object> filter) {

        String conditiontype = filter.get("conditiontype").toString();
        String value = filter.get("value").toString();
        if ("4".equals(conditiontype)) {
            String valueName = "";
            if ("1".equals(value)) {
                valueName = "信息";
            } else if ("2".equals(value)) {
                valueName = "警告";
            } else if ("3".equals(value)) {
                valueName = "一般严重";
            } else if ("4".equals(value)) {
                valueName = "严重";
            } else if ("5".equals(value)) {
                valueName = "灾难";
            } else {
                valueName = "未分类";
            }
            filter.put("valueName", valueName);
        } else {
            Zabbix zabbix = Zabbix.getZabbix();
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("output", "extend");
            String method = "";
            if ("0".equals(conditiontype)) {
                method = "hostgroup.get";
                params.put("groupids", value);
            } else if ("1".equals(conditiontype)) {
                method = "host.get";
                params.put("hostids", value);
            } else if ("2".equals(conditiontype)) {
                method = "trigger.get";
                params.put("triggerids", value);
            } else {
                filter.put("valueName", value);
                return;
            }
            Map<String, Object> result = zabbix.call(method, params);
            List<Map<String, Object>> mapList = (List<Map<String, Object>>) result.get("result");
            if ("2".equals(conditiontype)) {
                filter.put("valueName", mapList.get(0).get("description"));
            } else {
                filter.put("valueName", mapList.get(0).get("name"));

            }

        }

    }

    /**
     * 创建(trigger类型)动作
     */
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public List<Map<String, Object>> createAction(@RequestBody Map form) {
        Zabbix zabbix = Zabbix.getZabbix();
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("name", "Trigger action");
        params.put("eventsource", "0");
        params.put("status", "0");
        params.put("esc_period", "120");
        params.put("def_shortdata", "{TRIGGER.NAME}: {TRIGGER.STATUS}");
        params.put("def_longdata", "{TRIGGER.NAME}: {TRIGGER.STATUS}\\r\\nLast value: {ITEM.LASTVALUE}\\r\\n\\r\\n{TRIGGER.URL}");

        Map<String, Object> result = zabbix.call("action.create", params);
        List<Map<String, Object>> mapList = (List<Map<String, Object>>) result.get("result");
        return mapList;
    }

    /**
     * 修改(trigger类型)动作
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public void updateAction(@RequestBody Map params) {
        Zabbix zabbix = Zabbix.getZabbix();
        zabbix.call("action.update", params);
    }

    /**
     * @param status
     * @return
     */
    @RequestMapping(value = "/updateStatus/{actionId}/{status}", method = RequestMethod.POST)
    public void updateStatus(@PathVariable() String actionId, @PathVariable() String status) {
        Zabbix zabbix = Zabbix.getZabbix();
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("actionid", actionId);
        params.put("status", status);
        zabbix.call("action.update", params);
    }

    /**
     * 删除(trigger类型)动作
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public List<Map<String, Object>> deleteAction(@RequestBody Map form) {
        Zabbix zabbix = Zabbix.getZabbix();
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("actionid", new Object[]{"11", "12"});
        Map<String, Object> result = zabbix.call("action.delete", params);
        List<Map<String, Object>> mapList = (List<Map<String, Object>>) result.get("result");
        return mapList;
    }
}
